package com.shenqi.language.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.shenqi.language.domain.InvitationCode;
import com.shenqi.language.mapper.InvitationCodeMapper;
import com.shenqi.language.util.TimeParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shenqi.language.mapper.UservipMapper;
import com.shenqi.language.domain.Uservip;
import com.shenqi.language.service.IUservipService;

/**
 * VIP信息Service业务层处理
 * 
 * @author shenqi
 * @date 2025-08-05
 */
@Service
public class UservipServiceImpl implements IUservipService 
{
    @Autowired
    private UservipMapper uservipMapper;
    @Autowired
    private InvitationCodeMapper invitationCodeMapper;

    /**
     * 查询VIP信息
     * 
     * @param userId VIP信息主键
     * @return VIP信息
     */
    @Override
    public Uservip selectUservipByUserId(Long userId)
    {
        return uservipMapper.selectUservipByUserId(userId);
    }

    /**
     * 查询VIP信息列表
     * 
     * @param uservip VIP信息
     * @return VIP信息
     */
    @Override
    public List<Uservip> selectUservipList(Uservip uservip)
    {
        return uservipMapper.selectUservipList(uservip);
    }

    /**
     * 新增VIP信息
     * 
     * @param uservip VIP信息
     * @return 结果
     */
    @Override
    public int insertUservip(Uservip uservip)
    {
        return uservipMapper.insertUservip(uservip);
    }

    /**
     * 修改VIP信息
     * 
     * @param uservip VIP信息
     * @return 结果
     */
    @Override
    public int updateUservip(Uservip uservip)
    {
        return uservipMapper.updateUservip(uservip);
    }

    /**
     * 批量删除VIP信息
     * 
     * @param userIds 需要删除的VIP信息主键
     * @return 结果
     */
    @Override
    public int deleteUservipByUserIds(Long[] userIds)
    {
        return uservipMapper.deleteUservipByUserIds(userIds);
    }

    /**
     * 删除VIP信息信息
     * 
     * @param userId VIP信息主键
     * @return 结果
     */
    @Override
    public int deleteUservipByUserId(Long userId)
    {
        return uservipMapper.deleteUservipByUserId(userId);
    }


    @Override
    public void useInvitationCode(String invitationCode, Long userId) {
        InvitationCode code = invitationCodeMapper.selectInvitationCodeByCode(invitationCode);
        if(code.getUserId() != null) {
            throw new ServiceException(String.format("激活码:[%s]已经用过的了。", invitationCode));
        }
        // 设置这个码被用户使用了。
        code.setUserId(userId);
        invitationCodeMapper.updateInvitationCode(code);
        // 查看VIP信息，更新VIP有效时间。
        Uservip userVip = uservipMapper.selectUservipByUserId(userId);
        if (userVip == null) {
            userVip = new Uservip();
            userVip.setUserId(userId);
            // 级别1，暂时没有什么用。
            userVip.setVipLevel(1);
            uservipMapper.insertUservip(userVip);
        }
        Date endTime = userVip.getEndTime();
        Date endDate = TimeParser.calculateEndTime(code.getProduct().getEffect(), endTime);
        userVip.setEndTime(endDate);
        uservipMapper.updateUservip(userVip);
    }
}
